package com.hg.chapter861.controller;

import com.hg.chapter861.enetity.Location;
import com.hg.chapter861.enetity.Person;
import com.hg.chapter861.repoitory.PersonRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedList;
import java.util.List;

@RestController
public class PersonController {

    @Autowired
    PersonRepository personRepository;

    @PostMapping("/save")
    public Person save() {
        Location location1 = new Location("aaa", "aaa");
        Location location2 = new Location("bbb", "bbb");
        Location location3 = new Location("ccc", "ddd");
        LinkedList<Location> locations = new LinkedList<>();
        locations.add(location1);
        locations.add(location2);
        locations.add(location3);

        Person p = new Person("a", "aa", 20, locations);
        return personRepository.save(p);
    }

    @GetMapping("/name")
    public Person name(){
        return personRepository.findByName("aa");
    }

    @GetMapping("/age")
    public List<Person> age(){
        return personRepository.withQueryFindByAge(20);
    }
}
